/**
 * @author chuzhixin 1204505056@qq.com （不想保留author可删除）
 * @description 路由拦截状态管理，目前两种模式：all模式与intelligence模式，其中partialRoutes是菜单暂未使用
 */
import router from '@/router'
import { asyncRoutes, constantRoutes, resetRouter } from '@/router'
import { GetUserMenuAccessRouter } from '@/api/systemManage/userInfo'
import { convertRouter, filterAsyncRoutes } from '@/utils/handleRoutes'

const state = () => ({
  navbarMenus: [],
  routes: [],
  partialRoutes: [],
  routePageLoading: false,
})
const getters = {
  navbarMenus: (state) => state.navbarMenus,
  routes: (state) => state.routes,
  partialRoutes: (state) => state.partialRoutes,
  routePageLoading: (state) => state.routePageLoading,
}
const mutations = {
  setPageLoading(state, loading) {
    state.routePageLoading = loading
  },
  setRoutes(state, routes) {
    state.routes = constantRoutes.concat(routes)
  },
  setAllRoutes(state, routes) {
    state.routes = constantRoutes.concat(routes)
  },
  setNavbarMenus(state, menus) {
    state.navbarMenus = menus
  },
  setPartialRoutes(state, routes) {
    state.partialRoutes = constantRoutes.concat(routes)
  },
}
const actions = {
  async setPageLoadingState({ commit }, isLoading) {
    commit('setPageLoading', isLoading)
  },
  // --------- 以下方法好像没用 ---------------
  async setRoutes({ commit }, permissions) {
    //开源版只过滤动态路由permissions，admin不再默认拥有全部权限
    const finallyAsyncRoutes = await filterAsyncRoutes(
      [...asyncRoutes],
      permissions
    )
    commit('setRoutes', finallyAsyncRoutes)
    // console.log('没用的方法， setRoutes')
    return finallyAsyncRoutes
  },
  async setAllRoutes({ commit }) {
    // let { data } = await getRouterList()
    console.log('setAllRoutes')
    // let { data } = await getRouterList({ token: getAccessToken() }) // 请求菜单带上token
    data.push({ path: '*', redirect: '/404', hidden: true })
    let accessRoutes = convertRouter(data)
    commit('setAllRoutes', accessRoutes)
    // console.log('没用的方法， setAllRoutes')
    return accessRoutes
  },
  setPartialRoutes({ commit }, accessRoutes) {
    commit('setPartialRoutes', accessRoutes)
    // console.log('没用的方法， setPartialRoutes')
    return accessRoutes
  },
  // --------- 以上方法好像没用 ---------------

  // 获取顶部一级菜单与默认左侧子菜单
  async setNavigateMenus({ commit }) {
    let { data } = await GetUserMenuAccessRouter() // 此处应该在获取userInfo之后，才有token，才能得到正确的数据
    // console.log(data)
    // console.log('setNavigateMenus')
    // 遍历取出第一个的子菜单，做出路由对象返回
    let accessRoutes = []
    if (data && data.length > 0) {
      let sideMenus = JSON.parse(JSON.stringify(data[0].children)) // 必须复制，否则点击navbar无法重新制作路由
      accessRoutes = convertRouter(sideMenus)
    }
    commit('setNavbarMenus', data)
    commit('setAllRoutes', accessRoutes)
    return accessRoutes
  },

  // 获取菜单的子菜单，做出路由返回
  async setSideBarMenus({ commit }, navMenuItem){
    return new Promise((resolve, reject) => {
      resetRouter(router)
      let accessRoutes = []
      if (navMenuItem.children && navMenuItem.children.length > 0) {
        let sideMenus = JSON.parse(JSON.stringify(navMenuItem.children)) // 必须复制，否则点击navbar无法重新制作路由
        accessRoutes = convertRouter(sideMenus)
      }
      commit('setAllRoutes', accessRoutes)
      accessRoutes.forEach((item) => {
        router.addRoute(item)
      })
      resolve(accessRoutes)
    })
  },
}
export default { state, getters, mutations, actions }
